﻿@{
    ViewBag.Title = "2眼看书";
}

<div class="chapter">
    <div class="categoryMenu">
        <a href="@Url.Action("Category", "Book", new { typeid=1 })">玄幻魔法</a>
        <a href="@Url.Action("Category", "Book", new { typeid=2 })">武侠修真</a>
        <a href="@Url.Action("Category", "Book", new { typeid=3 })">都市言情</a>
        <a href="@Url.Action("Category", "Book", new { typeid=4 })">历史穿越</a>
        <a href="@Url.Action("Category", "Book", new { typeid=5 })">恐怖悬疑</a>
        <a href="@Url.Action("Category", "Book", new { typeid=6 })">游戏竞技</a>
        <a href="@Url.Action("Category", "Book", new { typeid=7 })">军事科幻</a>
        <a href="@Url.Action("Category", "Book", new { typeid=8 })">女生频道</a>
    </div>
</div>
<div class="chapter lastRead" style="display:none;">
    <p>
        <i class="am-icon-paperclip"></i>
        最近阅读
    </p>
    <div>
    </div>
</div>

<div class="chapter">
    <form method="get" class="search-form" action="@Url.Action("Search")">
        <input class="search-input" type="search" name="keyword" value="@ViewBag.Searchword" placeholder="输入小说名或作者名" />
        <button type="submit" class="search-btn">搜索</button>
    </form>
</div>

<div class="chapter am-hide">
    <dl class="am-cf sorttitle">
        <dt>随机推荐</dt>
        <dd><i class="am-icon-refresh"></i>&nbsp;换一换</dd>
    </dl>

    <div class="tuijian am-g">
        <div class="am-u-sm-6 am-u-md-3">
            <a href="#" class="tuijian-item">
                <dl>
                    <dt>小说名字</dt>
                    <dd>作者</dd>
                </dl>
                <div class="tuijian-item-desc">
                    从饥饿中苏醒，他只有一个信念，在这废土世界活下去……
                </div>
            </a>
        </div>   
        <div class="am-u-sm-6 am-u-md-3">
            <a href="#" class="tuijian-item">
                <dl>
                    <dt>小说名字</dt>
                    <dd>作者</dd>
                </dl>
                <div class="tuijian-item-desc">
                    从饥饿中苏醒，他只有一个信念，在这废土世界活下去……
                </div>
            </a>
        </div>
    </div>

</div>

<div class="am-g" id="CategoryList"> 
</div>

@section Scripts{
    <script>
        $(function () {
            var hitoryList = userHistory.fetch();
            if (hitoryList && hitoryList.length > 0) {
                var item = hitoryList[0];
                var temp = "<a href=\"/book/Chapter/" + item.bookid + "/" + item.chapterid + "\">";
                temp += item.bookname + "" + item.chapterName;
                temp += "<i class=\"am-icon-angle-double-right am-fr\"></i>";
                temp += "</a>";

                $(".lastRead div").html(temp);
                $(".lastRead").show();
            }
        })
    </script>

    <script>
        $(function () {            
            //HomePage的数据保存到本地localStore里，超过过期时间再从数据库里取
            var strHomeData = localStorage.getItem("homeData");            
            if (strHomeData)
            {
                var homeData = JSON.parse(strHomeData);
                var updateTime = new Date(homeData.updateTime);
                if (new Date().getTime() - updateTime.getTime() > 86400) {                    
                    loadHomePage();
                }
                else
                {                    
                    var temp = "";
                    homeData.data.forEach(function (item) {
                        temp += "<div class=\"categoryList am-u-sm-12 am-u-md-6 am-u-end\">";
                        temp += "<dl class=\"am-cf sorttitle\">";
                        temp += "<dt>" + item.typeName + "</dt>";
                        temp += "<dd><a href=\"/Book/Category/" + item.typeid + "\">更多&gt;&gt;</a></dd>";
                        temp += "</dl>";
                        temp += "<ul class=\"am-list\">";
                        item.list.forEach(function (book) {
                            temp += "<li>";
                            temp += " <a href=\"/Book/ChapterList/" + book.Id + "\">";
                            temp += book.BookName;
                            temp += "<i>[" + book.Author + "]</i>";
                            temp += "</a>";
                            temp += "</li>";
                        })
                        temp += "</ul>";
                        temp += "</div>";
                    })
                    $("#CategoryList").html(temp);
                }
              
            }
            else
            {
                loadHomePage();                
            }

        })
        function loadHomePage() {
            $.ajax({
                type: "POST",
                url: '@Url.Action("HomePageData", "Book")',
                dataType: "json",
                success: function (data) {                    
                    var homeData = {
                        updateTime: new Date(),
                        data: data
                    };
                    localStorage.setItem("homeData", JSON.stringify(homeData));
                    var temp = "";
                    data.forEach(function (item) {
                        temp += "<div class=\"categoryList am-u-sm-12 am-u-md-6 am-u-end\">";
                        temp += "<dl class=\"am-cf sorttitle\">";
                        temp += "<dt>" + item.typeName + "</dt>";
                        temp += "<dd><a href=\"/Book/Category/" + item.typeid + "\">更多&gt;&gt;</a></dd>";
                        temp += "</dl>";
                        temp += "<ul class=\"am-list\">";
                        item.list.forEach(function (book) {
                            temp += "<li>";
                            temp += " <a href=\"/Book/ChapterList/" + book.Id + "\">";
                            temp += book.BookName;
                            temp += "<i>[" + book.Author + "]</i>";
                            temp += "</a>";
                            temp += "</li>";
                        })
                        temp += "</ul>";
                        temp += "</div>";
                    })
                    $("#CategoryList").html(temp);
                },
                beforeSend: function () {
                },
                complete: function () {
                }
            });
        }
      
    </script>
}


